package com.hyt.it.ogt.kq.service.gov.api.ks;

import com.alibaba.fastjson.JSON;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.core.util.FastJsonUtil;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.service.gov.api.ks.model.*;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.vo.Tree;
import com.hyt.it.ogt.kq.service.gov.service.ITaskCandidateAreaService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeRoomCandidateService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeRoomExaminerService;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考试服务调用的接口
 * </p>
 *
 * @author chengxh
 * @since 2021-11-23
 */
@RestController
@Api(tags = "25.考试服务调用的接口", value = "考试服务调用的接口")
@ApiSort(value = 25)
@RequestMapping("/api/ks")
@Slf4j
public class KsApi {

	@Resource
	private ITaskService iTaskService;

	@Resource
	private ITimeRoomExaminerService iTimeRoomExaminerService;

	@Resource
	private ITaskCandidateAreaService iTaskCandidateAreaService;

	@Resource
	private ITimeRoomCandidateService iTimeRoomCandidateService;

    @ApiOperation(
            value = "25.1 获取用户下所有的任务信息",
            notes = "30000:成功"
    )
    @ApiOperationSort(value = 1)
    @PostMapping("/getExamTask")
    public ApiResponse<Object> getExamTask(@RequestBody QueryExamTaskParam queryExamTaskParam) {
		log.info("# 获取用户下所有的任务信息参数 ： {}", FastJsonUtil.getBeanToJson(queryExamTaskParam));
		try {
			if(StringUtils.isBlank(queryExamTaskParam.getUserId()) || StringUtils.isBlank(queryExamTaskParam.getOfficeId())
					|| StringUtils.isBlank(queryExamTaskParam.getUserType())) {
				throw new KqException(ResponseCode.ERROR_KS_GET_TASK_PARAM.getCode(),
						ResponseCode.ERROR_KS_GET_TASK_PARAM.getMsg());
			}
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(iTaskService.getExamTask(queryExamTaskParam))
					.build();
		} catch (KqException i) {
			log.error("# 获取用户下所有的任务信息参数异常: {}", i.getErrMsg());
			return ApiResponse.builder()
					.code(i.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 获取用户下所有的任务信息参数异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_KS_GET_TASK.getCode())
					.build();
		}
    }

	@ApiOperation(
			value = "25.2 获取考务人员和考场的关系",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 2)
	@PostMapping("/getUserRoom")
	public ApiResponse<Object> getUserRoom(@RequestBody QueryUserRoomParam queryUserRoomParam) {
		log.info("# 获取考务人员和考场的关系参数 ： {}", JSON.toJSONString(queryUserRoomParam));

		try {
			if(StringUtils.isBlank(queryUserRoomParam.getUserId()) || StringUtils.isBlank(queryUserRoomParam.getOfficeId())
					|| StringUtils.isBlank(queryUserRoomParam.getUserType()) || StringUtils.isBlank(queryUserRoomParam.getTimeId())) {
				throw new KqException(ResponseCode.ERROR_KS_GET_USER_ROOM_PARAM.getCode(),
						ResponseCode.ERROR_KS_GET_USER_ROOM_PARAM.getMsg());
			}
			List<UserRoomVO> result = iTimeRoomExaminerService.getUserRoom(queryUserRoomParam);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(result)
					.build();
		} catch (KqException i) {
			log.error("#获取考务人员和考场的关系异常: {}", i.getErrMsg());
			return ApiResponse.builder()
					.code(i.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 获取考务人员和考场的关系异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_KS_GET_USER_ROOM.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "25.3 获取考务人员和批次的关系",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 3)
	@PostMapping("/getUserTime")
	public ApiResponse<Object> getUserTime(@RequestBody QueryUserTimeParam queryUserTimeParam) {
		log.info("# 获取考务人员和批次的关系参数 ： {}", queryUserTimeParam);

		try {
			if(StringUtils.isBlank(queryUserTimeParam.getUserId()) || StringUtils.isBlank(queryUserTimeParam.getOfficeId())
					|| StringUtils.isBlank(queryUserTimeParam.getUserType()) || StringUtils.isBlank(queryUserTimeParam.getTaskId())) {
				throw new KqException(ResponseCode.ERROR_KS_GET_USER_TIME_PARAM.getCode(),ResponseCode.ERROR_KS_GET_USER_TIME_PARAM.getMsg());
			}

			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(iTimeRoomExaminerService.getUserTime(queryUserTimeParam))
					.build();
		} catch (KqException i) {
			log.error("# 获取考务人员和批次的关系异常: {}", i.getErrMsg());
			return ApiResponse.builder()
					.code(i.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 获取考务人员和批次的关系异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_KS_GET_USER_TIME.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "25.4 获取任务基本信息",
			notes = "30000:成功"
	)
	@ApiOperationSort(value = 4)
	@GetMapping("/getTaskInfo")
	public ApiResponse<Object> getTaskInfo(String taskId) {
		log.info("# 获取任务基本信息 ： {}", taskId);
		try {

			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(iTaskService.getTaskInfoById(taskId))
					.build();
		} catch (KqException i) {
			log.error("# 获取任务基本信息异常: {}", i.getErrMsg());
			return ApiResponse.builder()
					.code(i.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 获取任务基本信息异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_KS_GET_TASK.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "25.5 通过考试任务id、批次id、准考证号获取考生区域信息",
			notes = "30000:成功; 43250501:通过考试任务id、批次id、准考证号获取考生区域信息异常"
	)
	@ApiOperationSort(value = 5)
	@ApiImplicitParams({
			@ApiImplicitParam(
					name = "taskId",
					value = "考试任务id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			),
			@ApiImplicitParam(
					name = "timeId",
					value = "批次id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			),
			@ApiImplicitParam(
					name = "admissionNumber",
					value = "准考证号",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			)
	})
	@GetMapping("/getCandidateArea")
	public ApiResponse<CandidateAreaVO> getCandidateArea(@RequestParam String taskId,
														 @RequestParam String timeId,
														 @RequestParam String admissionNumber) {
    	log.info("# 通过考试任务id、批次id、准考证号获取考生区域信息参数: {}, {}, {}", taskId, timeId, admissionNumber);
    	try {
			CandidateAreaVO candidateAreaVO = iTaskCandidateAreaService.getVOByTaskId(taskId, timeId, admissionNumber);
			return ApiResponse.<CandidateAreaVO>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(candidateAreaVO)
					.build();
		} catch (KqException k) {
    		log.error("通过考试任务id、批次id、准考证号获取考生区域信息业务异常: {}", k.getErrMsg());
			return ApiResponse.<CandidateAreaVO>builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("通过考试任务id、批次id、准考证号获取考生区域信息异常: ", e);
			return ApiResponse.<CandidateAreaVO>builder()
					.code(ResponseCode.GET_CANDIDATE_AREA_EXCEPTION.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "25.6 通过任务id和批次id获取现考现评编排数据",
			notes = "30000:成功; "
	)
	@ApiOperationSort(value = 6)
	@ApiImplicitParams({
			@ApiImplicitParam(
					name = "taskId",
					value = "考试任务id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			),
			@ApiImplicitParam(
					name = "timeId",
					value = "批次id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			)
	})
	@GetMapping("/getTaskCandidateArrange")
	public ApiResponse<NowTestNowDataVO> getCandidateArrange(@RequestParam String taskId, @RequestParam String timeId) {
		log.info("# 通过任务id和批次id获取现考现评编排数据参数: {}, {}", taskId, timeId);
		try {

			return ApiResponse.<NowTestNowDataVO>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(iTimeRoomCandidateService.getCandidateArrange(taskId,timeId))
					.build();
		} catch (KqException k) {
			log.error("通过任务id和批次id获取现考现评编排数据异常: {}", k.getErrMsg());
			return ApiResponse.<NowTestNowDataVO>builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("通过任务id和批次id获取现考现评编排数据异常: ", e);
			return ApiResponse.<NowTestNowDataVO>builder()
					.code(ResponseCode.GET_CANDIDATE_ARRANGE_EXCEPTION.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "25.7 通过任务获取考试分类",
			notes = "30000:成功; "
	)
	@ApiOperationSort(value = 7)
	@ApiImplicitParams({
			@ApiImplicitParam(
					name = "taskId",
					value = "考试任务id",
					dataType = DataType.STRING,
					paramType = ParamType.QUERY,
					required = true
			)
	})
	@GetMapping("/getTaskExamType")
	public ApiResponse<NowTestNowDataVO> getTaskExamType(@RequestParam String taskId) {
		log.info("# 通过任务获取考试分类参数: {}", taskId);
		try {

			return ApiResponse.<NowTestNowDataVO>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(iTimeRoomCandidateService.getTaskExamType(taskId))
					.build();
		} catch (KqException k) {
			log.error("通过任务获取考试分类异常: {}", k.getErrMsg());
			return ApiResponse.<NowTestNowDataVO>builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("通过任务获取考试分类异常: ", e);
			return ApiResponse.<NowTestNowDataVO>builder()
					.code(ResponseCode.GET_TEST_CLASSIFY_EXCEPTION.getCode())
					.build();
		}
	}


	@ApiOperation(
			value = "25.8 获取用户权限范围内考点考场树",
			notes = "30000:成功; "
	)
	@ApiOperationSort(value = 8)
	@ApiVersion(group = ApiVersionConstant.V2_4_10)
	@PostMapping("/getUserPlaceRoom")
	public ApiResponse<Object> getUserPlaceRoom(@RequestBody QueryUserRoomParam queryUserRoomParam) {
		log.info("# 获取考务人员和考场树型结构的关系参数 ： {}", JSON.toJSONString(queryUserRoomParam));

		try {
			if(StringUtils.isBlank(queryUserRoomParam.getUserId()) || StringUtils.isBlank(queryUserRoomParam.getOfficeId())
					|| StringUtils.isBlank(queryUserRoomParam.getUserType()) || StringUtils.isBlank(queryUserRoomParam.getTimeId())) {
				throw new KqException(ResponseCode.ERROR_KS_GET_USER_ROOM_PARAM.getCode(),
						ResponseCode.ERROR_KS_GET_USER_ROOM_PARAM.getMsg());
			}
			List<UserRoomVO> result = iTimeRoomExaminerService.getUserRoom(queryUserRoomParam);
			List<Tree> treeList = new ArrayList<>();
			if(null == result) {
			    return ApiResponse.builder()
	                    .code(ResponseCode.SUCCESS.getCode())
	                    .data(treeList)
	                    .build();
			}
			treeList = iTimeRoomExaminerService.buildTree(result);
			log.info("# 返回获取考务人员和考场树型结构的关系结果 ： {}", JSON.toJSONString(treeList));
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(treeList)
					.build();
		} catch (KqException i) {
			log.error("#获取考务人员和考场的关系异常: {}", i.getErrMsg());
			return ApiResponse.builder()
					.code(i.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 获取考务人员和考场的关系异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_KS_GET_USER_ROOM.getCode())
					.build();
		}
	}

}
